package debugServer;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.BadLocationException;

import YAPserver.ClientHandler;
import YAPserver.Lobby;

public class MiscPanel extends JPanel{
	MiscCode selection;
	ClientHandler player;
	double argument;
	Lobby lobby;
	
	JButton performButton = new JButton("perform");
	JList<MiscCode> miscList = new JList<MiscCode>(MiscCode.values());
	JList<ClientHandler> playerList;
	JScrollPane miscPane = new JScrollPane(miscList);
	JScrollPane playerPane;
	JPanel buttonPanel = new JPanel(new BorderLayout());
	JPanel textPanel = new JPanel(new BorderLayout());
	JTextField textField = new JTextField();
	
	MiscPanel(Lobby lobby){
		this.lobby = lobby;
		createPlayerList();
		createPanel();
	}
	
	private void createPlayerList() {
		ClientHandler[] clients = new ClientHandler[lobby.getPlayers().size()];
		int i=0;
		lobby.getPlayers().toArray(clients);
		playerList = new JList<ClientHandler>(clients);
		playerPane = new JScrollPane(playerList);
	}

	private void createPanel(){
		this.setLayout(new GridLayout(1, 4));
		performButton.addActionListener(new PerformListener());
		buttonPanel.add(performButton, BorderLayout.CENTER);
		
		miscPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		miscPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		miscList.setVisibleRowCount(4);
		miscList.setSelectedIndex(0);
		miscList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		miscList.addListSelectionListener(new MiscListListener());
		
		playerPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		playerPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		playerList.setVisibleRowCount(4);
		playerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		playerList.setSelectedIndex(0);
		playerList.addListSelectionListener(new PlayerListListener());
		
		textField.addActionListener(new TextListener());
		textField.setEditable(true);
		textField.setColumns(10);
//		textPanel.add(textField);
		this.add(miscPane);
		this.add(textField);
		this.add(playerPane);
		this.add(buttonPanel);
		
		//make sure that something is selected
		selection = miscList.getSelectedValue();
		player = playerList.getSelectedValue();
		argument = 0;
	}
	
	class PerformListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if(textField.getText()!=null){
				String s = textField.getText();
				if(s.isEmpty() == false){
					argument = Double.parseDouble(s);
				}
			}
			DebugConsole.perform(selection, lobby.getGame(), argument, player.getGameId());
		}
		
	}
	
	class MiscListListener implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent e) {
			selection = miscList.getSelectedValue();
		}
		
	}
	
	class PlayerListListener implements ListSelectionListener{
		@Override
		public void valueChanged(ListSelectionEvent e) {
			player = playerList.getSelectedValue();
		}
	}
	
	class TextListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(textField.getText()!=null){
				String s = textField.getText();
				argument = Double.parseDouble(s);
			}
		}
	}
}
